﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UniumLocalNetGames.Net;

namespace UniumLocalNetGames
{
    public partial class MainForm : Form
    {
        private UniumLocalUdpChat chat;
        public MainForm()
        {
            InitializeComponent();
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            var settingsForm = new SettingsForm();
            settingsForm.Chat = chat;
            settingsForm.Show();
            
        }
        /// <summary>
        /// ЗАгрузка Мейн ФОрм
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MainForm_Load(object sender, EventArgs e)
        {            
            
            chat = new UniumLocalUdpChat();
            chat.IncomingMessage += (s, args) => OnChat_IncomingMessage(args);

            ContextMenuClients = new ContextMenuStrip();
            ContextMenuClients.Opening += new CancelEventHandler(ContextMenuClients_Opening);
            LBClients.ContextMenuStrip = ContextMenuClients;
            foreach (var kvp in chat._onlineClients)
            {
                for (int i = 0; i < 3; i++)
                {
                    LBClients.Items.Add(kvp.Value);

                }
            }
            for (int i = 0; i < 3; i++)
            {
                LBClients.Items.Add("Item: " + i);

            }

           
        }
        /// <summary>
        /// Входящее сообщение
        /// </summary>
        /// <param name="message"></param>
        private void OnChat_IncomingMessage(IncomingMessageEventArgs message)
        {
           
        }

        private void LBClients_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                LBClients.SelectedIndex = LBClients.IndexFromPoint(e.Location);
                if (LBClients.SelectedIndex != -1)
                {
                    ContextMenuClients.Show();
                }
            }
        }
        public void InviteToPrivate(int index)
        {
             PrivateForm privat = new PrivateForm();
             privat.Show();

        }
              
        /// <summary>
        /// Контекстное меню
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
   
        private void ContextMenuClients_Opening(object sender, CancelEventArgs e)
        {
            ContextMenuClients.Items.Clear();
            ContextMenuClients.Items.Add(string.Format("Приватная беседа"));
            ContextMenuClients.Items[0].Click += (s, args)=>InviteToPrivate(LBClients.SelectedIndex);
           
        }

        private void LBClients_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void ButConnect_Click(object sender, EventArgs e)
        {
            chat = new UniumLocalUdpChat();
            chat.StartListen();
            ButConnect.Enabled = false;
        }


    }
}
